#include "cthermometer.h"
#include <QPainter>
#include <QDebug>

CThermometer::CThermometer(QWidget *parent) : QWidget(parent)
{
    m_fValue = 0.0f;

    m_pDataThread = new CDataThread;
    m_pDataThread->start();
    connect(m_pDataThread, &CDataThread::sig_UpdateWenduData, this, &CThermometer::getWenValue);
}

void CThermometer::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //将温度计图片在这里绘制出来
    QPixmap pixmap(QPixmap("://image//wenduji.png"));
    painter.drawPixmap(QRect(0,0,56,200), pixmap);

    int rectHeight =(int) (m_fValue * 130);
    float rate = 255.0 / 130.0;
    painter.save();
    painter.setPen(QPen(QColor(242,93,95),1));
    painter.setBrush(QColor(242,93,95));
    //painter.setBrush(QColor(rectHeight * rate,93,95));  //设置变换的颜色，但是效果不太好实现

    //绘制温度变化的矩形
    painter.drawRect(19,162 - rectHeight,25,rectHeight);
    painter.restore();
}

void CThermometer::getWenValue(float value)
{
    m_fValue = value;
    emit sig_SendValue(m_fValue);

    update(); //更新窗口，调用paintEvent
}
